﻿import game.Common;
import game.Letter;
import game.Main;
import game.SandClock;
import game.Session;
import mx.data.encoders.Num;
import mx.utils.Delegate;
/**
* ...
* @author Default
*/

class game.Word {

	private var _aLetters:Array;
	//private var _cLastAdded:Letter;
	private var _mcContainer:MovieClip;
	public function get lastJumped():Number { return Session.getInstance()._cCurrentMatch._firstWord.lastAdded != null ? Session.getInstance()._cCurrentMatch._firstWord.lastAdded._nInitialIndex : -1; };
	public var _nPreviousJumped:Number = -1;
	public var _toSecond:Number = 1;
	
	public function get length():Number { return _aLetters.length; }
	
	public function get lastAdded():Letter { return _aLetters.length > 0 ? _aLetters[_aLetters.length - 1] : null; }
	
	public function Word( sWord:String, mcContainer:MovieClip ) {
		_mcContainer = mcContainer;
		
		_aLetters = new Array();
		
		if (sWord != "") {
			createWord(sWord);
		}
	}
	
	public function wordJumped(index:Number) {
		
		if (_nPreviousJumped + 1 != index) {
			for (var i:Number = _nPreviousJumped + 1; i < index  ; i++) {
				Letter(_aLetters[i])._nCurrentIndex = Session.getInstance()._cCurrentMatch._secondWord.addLetter( Letter(_aLetters[i]) );
				
				SandClock.create(10 * _toSecond,  Delegate.create(Letter(_aLetters[i]), Letter(_aLetters[i]).jumpToSecond ) );
				_toSecond++;
			}
			_nPreviousJumped = index;
		} else {
			_nPreviousJumped = index;
		}
	}
	
	public function goBack() {
		
		if (Main.getInstance()._cGUI._canClick) {
			var letter:Letter = Session.getInstance()._cCurrentMatch._firstWord.lastAdded;
			letter = Letter(Session.getInstance()._cCurrentMatch._firstWord._aLetters.pop() );
		
			letter.reset();
			
			var goneBack:Number = letter._nInitialIndex;
			var lastLetter:Number = Session.getInstance()._cCurrentMatch._firstWord.lastAdded._nInitialIndex;
			lastLetter = lastLetter != undefined ? lastLetter : -1;
				
			if (lastLetter + 1 != goneBack) {
				
				for (var i:Number = lastLetter + 1; i < goneBack; i++) {
					var let:Letter = Session.getInstance()._cCurrentMatch._secondWord.lastAdded;
					let = Letter( Session.getInstance()._cCurrentMatch._secondWord._aLetters.pop() );
					let.reset();
				}
			}
		
			_nPreviousJumped = lastLetter;
		} else {
				Session.getInstance()._cCurrentMatch._originalWord._toSecond = 1;
			var lastLetter:Number = Session.getInstance()._cCurrentMatch._firstWord.lastAdded._nInitialIndex;
			lastLetter = lastLetter != undefined ? lastLetter : -1;
			var last:Number = lastAdded._nInitialIndex;
			for (var i:Number = lastLetter; i < last ; i++) {
					var let:Letter = Session.getInstance()._cCurrentMatch._secondWord.lastAdded;
					let = Letter( Session.getInstance()._cCurrentMatch._secondWord._aLetters.pop() );
					trace(let);
					let.reset();
			}
		//	_nPreviousJumped = lastLetter
					
		}
	}
		
	public function doJumpLeft():Void {
		trace(lastJumped)
		trace(_aLetters)
		for (var i:Number = lastJumped + 1; i < _aLetters.length  ; i++) {
				Letter(_aLetters[i])._nCurrentIndex = Session.getInstance()._cCurrentMatch._secondWord.addLetter(Letter(_aLetters[i]));
				Letter(_aLetters[i]).jumpToSecond();
		}
	}
	
	public function getWord():String {
		var _word:String = "";
		for (var i:Number = 0; i < _aLetters.length; i++) {
			_word += Letter(_aLetters[i])._sLetter;
		}
		
		return _word;
	}
	
	private function createWord(sWord:String):Void {
		var i:Number = 0;
		/*
		if (sWord.length > 0) {
			for (; i < 15; i++) {
				createLetter( "" );
			}
		}
		*/
		for (i = 0; i < sWord.length; i++) {
			createLetter( sWord.charAt(i) );
		} 
		Letter(_aLetters[ sWord.length -1 ]).onArrived = Delegate.create( Main.getInstance()._cGUI , Main.getInstance()._cGUI.hideEmptyTiles );
	}
	
	public function addLetter(cLetter:Letter):Number {
		_aLetters.push(cLetter); 
		return _aLetters.length - 1;
	}
	
	private function createLetter(_sLetter:String):Void {
		var _cLetter = new Letter(_sLetter, _mcContainer, _aLetters.length);
		
		addLetter(_cLetter);
	}
	
	public function start():Void {
		for (var i:Number = 0; i < _aLetters.length; i++) {
			Letter(_aLetters[i]).appear();
		}		
	}
	
	public function resetAll():Void {
		for (var i:Number = 0; i < _aLetters.length; i++) {
			Letter(_aLetters[i]).reset();
		}		
		_toSecond = 1;
	}
	
	public function removeAll():Void {
		Main.getInstance()._cGUI.showEmptyTiles();
		for (var i:Number = 0; i < _aLetters.length; i++) {
			Letter(_aLetters[i]).remove();
		}		
		_aLetters = null;
	}
	
}